package com.gy.smart;

import android.app.Application;
import android.graphics.Bitmap;
import android.util.DisplayMetrics;

import com.amap.api.location.AMapLocation;
import com.gy.smart.util.LocationClient;
import com.gy.smart.util.LocationManagers;
import com.gy.smart.util.LogUtil;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.utils.StorageUtils;

import org.xutils.x;

import java.io.File;

/**
 * 作者：gy on 17/1/4
 * 说明：
 */
public class MyApplication extends Application {

    public static int screenWidth;
    public static int screenHeight;
    public static float density;
    public static float curDensity;// 实际的density
    public static AMapLocation aMapLocation = null;

    @Override
    public void onCreate() {
        super.onCreate();
        getScreenSize();
        x.Ext.init(this);
        x.Ext.setDebug(true);
        initImageLoader();

        LocationClient.getInstance(getApplicationContext()).addLocationListener(new LocationClient.OnLocationListener() {
            @Override
            public void onAmapLocationListener(AMapLocation aMapLocation) {
                LogUtil.e(aMapLocation.getLatitude() +"  "+aMapLocation.getLongitude()
                        +"  "+ aMapLocation.getCity()+"  "+aMapLocation.getDistrict());
                MyApplication.this.aMapLocation = aMapLocation;
                LocationManagers.getInstance().notifys();
            }

            @Override
            public void onAmapLocationFail() {

            }
        });
    }


    /**
     * 获取屏幕分辨率方法
     */
    private void getScreenSize() {
        DisplayMetrics dm = new DisplayMetrics();
        dm = getApplicationContext().getResources().getDisplayMetrics();
        screenWidth = dm.widthPixels;
        screenHeight = dm.heightPixels;
        density = dm.density;
        curDensity = (float) screenWidth / 320f;
    }


    /**
     * 初始化imageLoader
     */
    private void initImageLoader() {
        File cacheDir = StorageUtils.getOwnCacheDirectory(getApplicationContext(), "imageloader/Cache");//图片缓存路径
        DisplayImageOptions imageOptions = new DisplayImageOptions.Builder().bitmapConfig(Bitmap.Config.RGB_565).build();
        ImageLoaderConfiguration config = new ImageLoaderConfiguration
                .Builder(getApplicationContext())
//                .memoryCacheExtraOptions(480, 800) // 保存的每个缓存文件的最大长宽
                .threadPoolSize(3)//线程池内加载的数量
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory()
                .memoryCache(new WeakMemoryCache())
                .memoryCacheSize(80 * 1024 * 1024)

                .diskCacheFileCount(200)
                .discCache(new UnlimitedDiskCache(cacheDir))//自定义缓存路径
                .defaultDisplayImageOptions(imageOptions)
                .imageDownloader(new BaseImageDownloader(getApplicationContext(), 5 * 1000, 30 * 1000)) // 超时时间
                //                .writeDebugLogs()
                .build();//开始构建
        ImageLoader.getInstance().init(config);
    }
}
